package com.ufo.store.management.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.ufo.store.management.R;

/**
 * Created by taoli on 2016/10/18 0018.
 */

public class DeleteItemDialog  extends Dialog implements View.OnClickListener{
    private Context context;
    private OnItemClick mListener = null;
    private TextView mTitleTv;
    private String mTips;

    public DeleteItemDialog(Context context) {
        super(context, R.style.select_dialog);
        this.context = context;
    }

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_deleteitem);
        mTitleTv = (TextView) findViewById(R.id.tv_title);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_ok).setOnClickListener(this);
        if( !TextUtils.isEmpty(mTips) ){
            mTitleTv.setText("确定删除" + mTips + "？");
        }
    }

    public void setTips( String s){
        mTips = s;
        if( mTitleTv != null && !TextUtils.isEmpty(mTips)) {
            mTitleTv.setText("确定删除" + mTips + "？");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                if(mListener!=null)
                    mListener.onItemClick(0);
                break;
            case R.id.tv_ok:
                if(mListener!=null)
                    mListener.onItemClick(1);
                break;
        }
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (null != DeleteItemDialog.this && DeleteItemDialog.this.isShowing()) {
                    DeleteItemDialog.this.dismiss();
                }
            }
        }, 100);
    }

    @Override
    public void setTitle(CharSequence title) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void show() {
        if (context != null && ((Activity)context).isFinishing()) {
            return;
        }
        super.show();
    }

    public void setItemClickListener( OnItemClick p){
        mListener = p;
    }

    public interface OnItemClick {
        public void onItemClick(int position);
    }
}